﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace cn.Util
{
    /// <summary>
    /// http请求
    /// </summary>
    public class HttpPost
    {
        /// <summary>
        /// 发起HTTP请求
        /// </summary>
        /// <param name="web"></param>
        /// <returns></returns>
        public  string Get(string web) 
        {
           return  Get(web, System.Text.Encoding.GetEncoding("UTF-8"));
        }

        /// <summary>
        /// 发起HTTP请求
        /// </summary>
        /// <param name="web"></param>
        /// <param name="encodings"></param>
        /// <returns></returns>
        public string Get(string web, Encoding encodings)
        {
            string outs = "";
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(web);

            req.Method = "GET";
            using (WebResponse wr = req.GetResponse())
            {
                Stream newStream = wr.GetResponseStream();
                HttpWebResponse webResponse = (HttpWebResponse)wr;
                bool bRetVal = true;
                if (webResponse.StatusCode != HttpStatusCode.OK)
                    bRetVal = false;

                if (bRetVal)
                {
                    //创建接收回馈的字节流类
                    Stream receiveStream = webResponse.GetResponseStream();//得到回写的字节流
                    StreamReader readStream = new StreamReader(receiveStream, encodings);
                    outs = readStream.ReadToEnd();
                    readStream.Close();
                }
                if (webResponse != null)
                    webResponse.Close();
                //在这里对接收到的页面内容进行处理
                return outs;
            }

        }
    }
}
